package com.java复习.jdbc.sqlHelper;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class GetConn {

    private GetConn(){};

    private static Connection connection;

    public static Connection getConn(){
        if(connection==null){
            try{
                //1、加载数据库驱动程序
                Class.forName("com.mysql.cj.jdbc.Driver");

                //2、通过驱DriverManager获取数据库连接
                String url =
                        "jdbc:mysql://localhost:3306/book" +
                                "?user=root&password=ljh990124" +
                                "&useSSL=false&serverTimezone=UTC&useUnicode=true" +
                                "&characterEncoding=utf8";
                connection = DriverManager.getConnection(url);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return connection;
    }


    public static void closeConn(){
        if(connection!=null){
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

}
